package fr.uniscol.mvc;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Component
public class SimpleInterceptor implements HandlerInterceptor {

	private static Logger log = Logger.getLogger(SimpleInterceptor.class);
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		String req = request.getRequestURI() + " [";
		
		Enumeration<String> eParams = request.getParameterNames();
		String p = null;
		while(eParams.hasMoreElements()){
			p = eParams.nextElement();
			req += p;
			req += "=" + request.getParameter(p) + ",";
		}
		
		req += "]";
		
		log.info(req);
		
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}
}
